package com.lfy.modules.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lfy.modules.common.config.sys.DictionariesKey;
import com.lfy.modules.common.config.sys.SysDictionaries;
import com.lfy.modules.blog.domain.HomeDetail;
import com.lfy.modules.blog.domain.SocialEntity;
import com.lfy.modules.blog.domain.dto.home.PersonalDTO;
import com.lfy.modules.blog.mapper.UserEntityMapper;
import com.lfy.modules.blog.service.HomeDetailService;
import com.lfy.modules.blog.mapper.HomeDetailMapper;
import com.lfy.modules.blog.service.SocialService;
import com.lfy.modules.common.custom.FileService;
import com.lfy.modules.common.custom.constants.FileConstant;
import com.lfy.modules.common.domain.R;
import com.lfy.modules.common.domain.entity.UserEntity;
import com.lfy.modules.common.intercept.AuthHandlerInterceptor;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
* @author 吕飞扬
* @description 针对表【blog_home_detail】的数据库操作Service实现
* @createDate 2024-05-09 12:49:34
*/
@Service
public class HomeDetailServiceImpl extends ServiceImpl<HomeDetailMapper, HomeDetail>
    implements HomeDetailService{
    @Resource
    private FileService fileService;
    @Resource
    private UserEntityMapper userEntityMapper;
    @Resource
    private SocialService socialService;
    @Override
    public R<String> upHeaderImg(MultipartFile file) {
        UserEntity userEntity = AuthHandlerInterceptor.authLocal.get();
        String url = fileService.uploadSingle(file, FileConstant.builderUserHeadImgDir(userEntity.getId()));
        HomeDetail homeDetail = new HomeDetail();
        homeDetail.setAuthorHeaderImg(url);
        this.update(homeDetail,new LambdaQueryWrapper<HomeDetail>().eq(HomeDetail::getTenantId,userEntity.getId()));
        String prefix = SysDictionaries.getValue(DictionariesKey.SYS_MINIO_SITE);
        return R.success(prefix + url);
    }

    @Override
    public R<Void> modifyUserDetail(PersonalDTO dto) {
        HomeDetail homeDetail = new HomeDetail();
        BeanUtils.copyProperties(dto,homeDetail);
        UserEntity userEntity = AuthHandlerInterceptor.authLocal.get();
        this.baseMapper.update(homeDetail,new LambdaQueryWrapper<HomeDetail>().eq(HomeDetail::getTenantId,userEntity.getId()).eq(HomeDetail::getId,dto.getId()));;
        return R.success();

    }

    @Override
    public R<Void> upFm(MultipartFile file) {
        UserEntity userEntity = AuthHandlerInterceptor.authLocal.get();
        String url = fileService.uploadSingle(file, FileConstant.builderUserFmImgDir(userEntity.getId()));
        HomeDetail homeDetail = new HomeDetail();
        homeDetail.setFm(url);
        this.update(homeDetail,new LambdaQueryWrapper<HomeDetail>().eq(HomeDetail::getTenantId,userEntity.getId()));
        return R.success();
    }

    @Override
    public R<HomeDetail> getBlogTitle(String url) {

        UserEntity entity = userEntityMapper.selectOne(new LambdaQueryWrapper<UserEntity>().eq(UserEntity::getUrl, url));
        if (entity == null) {
            return R.error();
        }
        HomeDetail homeDetail = this.baseMapper.selectOne(new LambdaQueryWrapper<HomeDetail>().eq(HomeDetail::getTenantId, entity.getId()));
        String prefix = SysDictionaries.getValue(DictionariesKey.SYS_MINIO_SITE);
        homeDetail.setAuthorHeaderImg(prefix + homeDetail.getAuthorHeaderImg());
        return R.success(homeDetail);
    }

    @Override
    public R<SocialEntity> getSocialInfo(Long id) {
        SocialEntity social = socialService.getOne(Wrappers.lambdaQuery(SocialEntity.class).eq(SocialEntity::getTenantId, id));
        if (social == null) {
            return R.error("没有找到社交信息");
        }
        return R.success(social);
    }

    @Override
    public R<Void> modifySocialInfo(SocialEntity socialEntity, Long id) {
        socialService.update(socialEntity,Wrappers.lambdaQuery(SocialEntity.class).eq(SocialEntity::getTenantId, id));
        return R.success();
    }
}




